<?php

/**
 * Location Model
 * 
 * Location Related Model Fuction
 * Country Table, State Table, District Table, Locality Table
 * 
 * @author    Dino (DBK)
 * @date      30 Jan 2014
 * 
 */
class Location_model extends CI_Model {

  function __construct() {
    parent::__construct();
  }

  /**
   * Insert a new country
   * 
   * @param type $country_name
   * @return type
   */
  function put_country($country_name = null) {
    $insert = array();
    $insert['country_name'] = (string) $country_name;
    $this->db->insert('country', $insert);
    return (int) $this->db->insert_id();
  }

  /**
   * Get a country by name
   * 
   * @param type $country_name
   * @return type
   */
  function get_country($country_name = null) {
    $where = array();
    $where['country_name'] = (string) $country_name;
    return $this->db->select('*')
                    ->where($where)
                    ->get('country')
                    ->row_array();
  }

  /**
   * Insert a new state
   * 
   * @param type $state_name
   * @param type $country_id
   * @return type
   */
  function put_state($state_name = null, $country_id = 0) {
    $insert = array();
    $insert['state_name'] = (string) $state_name;
    $insert['state_country_id'] = (int) $country_id;
    $this->db->insert('state', $insert);
    return (int) $this->db->insert_id();
  }

  /**
   * Get a state by name
   * 
   * @param type $state_name
   * @return type
   */
  function get_state($state_name = null) {
    $where = array();
    $where['state_name'] = (string) $state_name;
    return $this->db->select('*')
                    ->where($where)
                    ->get('state')
                    ->row_array();
  }

  /**
   * Insert a new district
   * 
   * @param type $district_name
   * @param type $state_id
   * @return type
   */
  function put_district($district_name = null, $state_id = 0) {
    $insert = array();
    $insert['district_name'] = (string) $district_name;
    $insert['district_state_id'] = (int) $state_id;
    $this->db->insert('district', $insert);
    return (int) $this->db->insert_id();
  }

  /**
   * Get a district by name
   * 
   * @param type $district_name
   * @return type
   */
  function get_district($district_name = null) {
    $where = array();
    $where['district_name'] = (string) $district_name;
    return $this->db->select('*')
                    ->where($where)
                    ->get('district')
                    ->row_array();
  }

  /**
   * Insert a new locality
   * 
   * @param type $locality_name
   * @param type $district_id
   * @return type
   */
  function put_locality($locality_name = null, $district_id = 0) {
    $insert = array();
    $insert['locality_name'] = (string) $locality_name;
    $insert['locality_district_id'] = (int) $district_id;
    $this->db->insert('locality', $insert);
    return (int) $this->db->insert_id();
  }

  /**
   * Get a locality by name
   * 
   * @param type $locality_name
   * @return type
   */
  function get_locality($locality_name = null) {
    $where = array();
    $where['locality_name'] = (string) $locality_name;
    return $this->db->select('*')
                    ->where($where)
                    ->get('locality')
                    ->row_array();
  }

  /**
   * Get countries list of a given search term
   * 
   * @param type $search
   * @return type
   */
  function get_countries($search = null) {
    return $this->db->select('*')
                    ->like('country_name', (string) $search)
                    ->get('country')
                    ->result_array();
  }

  /**
   * Get state list of a given search term
   * 
   * @param type $search
   * @return type
   */
  function get_states($search = null) {
    return $this->db->select('*')
                    ->join('country', 'country_id = state_country_id')
                    ->like('state_name', (string) $search)
                    ->get('state')
                    ->result_array();
  }

  /**
   * Get districts list of a given search term
   * 
   * @param type $search
   * @return type
   */
  function get_districts($search = null) {
    return $this->db->select('*')
                    ->join('state', 'state_id = district_state_id')
                    ->join('country', 'country_id = state_country_id')
                    ->like('district_name', (string) $search)
                    ->get('district')
                    ->result_array();
  }

  /**
   * Get localities list of a given search term
   * 
   * @param type $search
   * @return type
   */
  function get_localities($search = null) {
    return $this->db->select('*')
                    ->join('district', 'district_id = locality_district_id')
                    ->join('state', 'state_id = district_state_id')
                    ->join('country', 'country_id = state_country_id')
                    ->like('locality_name', (string) $search)
                    ->get('locality')
                    ->result_array();
  }

  /**
   * Get a country by id
   * 
   * @param type $country_id
   * @return type
   */
  function get_country_id($country_id = 0) {
    $where = array();
    $where['country_id'] = (int) $country_id;
    return $this->db->select('*')
                    ->where($where)
                    ->get('country')
                    ->row_array();
  }

  /**
   * Get a state by id
   * 
   * @param type $state_id
   * @return type
   */
  function get_state_id($state_id = 0) {
    $where = array();
    $where['state_id'] = (int) $state_id;
    return $this->db->select('*')
                    ->where($where)
                    ->get('state')
                    ->row_array();
  }

  /**
   * Get a district by id
   * 
   * @param type $district_id
   * @return type
   */
  function get_district_id($district_id = 0) {
    $where = array();
    $where['district_id'] = (int) $district_id;
    return $this->db->select('*')
                    ->where($where)
                    ->get('district')
                    ->row_array();
  }

  /**
   * Get a locality by id
   * 
   * @param type $locality_id
   * @return type
   */
  function get_locality_id($locality_id = 0) {
    $where = array();
    $where['locality_id'] = (int) $locality_id;
    return $this->db->select('*')
                    ->where($where)
                    ->get('locality')
                    ->row_array();
  }

  /**
   * Get country and state by state id
   * 
   * @param type $state_id
   * @return type
   */
  function get_country_state_id($state_id = 0) {
    return $this->db->select('*')
                    ->join('country', 'country_id = state_country_id')
                    ->where('state_id', (int) $state_id)
                    ->get('state')
                    ->row_array();
  }

  /**
   * Get country, state and district by district id
   * 
   * @param type $district_id
   * @return type
   */
  function get_country_state_district_id($district_id = 0) {
    return $this->db->select('*')
                    ->join('state', 'state_id = district_state_id')
                    ->join('country', 'country_id = state_country_id')
                    ->where('district_id', (int) $district_id)
                    ->get('district')
                    ->row_array();
  }

  /**
   * Get country, state, district and locality by locality id
   * 
   * @param type $locality_id
   * @return type
   */
  function get_country_state_district_locality_id($locality_id = 0) {
    return $this->db->select('*')
                    ->join('district', 'district_id = locality_district_id')
                    ->join('state', 'state_id = district_state_id')
                    ->join('country', 'country_id = state_country_id')
                    ->where('locality_id', (int) $locality_id)
                    ->get('locality')
                    ->row_array();
  }

}
